package vesion2.tool;

import java.io.Serializable;

/**
 * 计时器
 */
public class Timer1 implements Serializable {
   private volatile boolean isRunning;
   private int state;
   private int oldState;
   private volatile int interval;
   public Timer1(){
       this(1000);
   }
   public Timer1(int interval){
       this.interval=interval;
       isRunning=false;
       state=1;
       oldState=0;
   }

   public  void Start(){
       if(interval<0||isRunning)
           return;
       isRunning=true;
       new Thread(()->{
           while (isRunning) {
               if(state>600000)
                   state=0;
               try {
                   Thread.sleep(interval);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
               ++state;
           }
       }
       ).start();
   }

   public boolean isOverOne(){
       if(state!=oldState){
           oldState=state;
           return true;
       }
       else
           return false;
   }

   public boolean isOverOneAndStar(){
       boolean re=isOverOne();
       if(re)
           Start();
       return re;
   }
    public void End() {
        isRunning = false;
    }

    public int getInterval() {
        return interval;
    }

    public  void setInterval(int interval) {
        this.interval = interval;
    }

}
